package com.qf.anno.ioc;

import com.qf.anno.web.interceptors.LoginInterceptor;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@ComponentScan("com.qf.anno.web")
@EnableWebMvc //相当于<mvc:annotation-driven/>
public class SpringMVCConfig implements WebMvcConfigurer {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable(); //<mvc:default-servlet-handler/>
    }

    /*
        相当于
                <mvc:resources mapping="/css/**" location="/css/"/>
                <mvc:resources mapping="/html/**" location="/html/"/>
                <mvc:resources mapping="/js/**" location="/js/"/>
                <mvc:resources mapping="/imgs/**" location="/imgs/"/>
     */
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/html/**").addResourceLocations("/html/");
//        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
//        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
//    }


    /*
        相当于：
            <mvc:interceptors>
                <mvc:interceptor>
                    <mvc:mapping path="/user/**"/>
                    <mvc:exclude-mapping path="/user/test1"/>
                    <mvc:exclude-mapping path="/user/login"/>
                    <bean class="com.qf2.interceptors.LoginInterceptor"/>
                </mvc:interceptor>
            </mvc:interceptors>
     */
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/user/login");
//    }


    /*
        相当于：
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="resourceViewResolver">
                <property name="prefix" value="/WEB-INF/html/"/>
                <property name="suffix" value=".html"/>
            </bean>
     */
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/html/");
        resolver.setSuffix(".html");
        registry.viewResolver(resolver);
    }
}
